<template>
  <el-container>
    <el-main>
      <el-alert
        v-if="modal.params.data.order_status_text || modal.params.data.order_fee"
        class="sa-alert sa-m-b-20"
      >
        <template #title>
          <div class="sa-m-b-4" v-if="modal.params.data.order_status_text">
            {{ modal.params.data.order_status_text }}
          </div>
          <div v-if="modal.params.data.order_fee">
            该订单存在改价，原应付金额：￥{{
              modal.params.data.order_fee.original_pay_fee
            }}，改价后支付金额：￥{{ modal.params.data.order_fee.pay_fee }}
          </div>
        </template>
      </el-alert>
      <el-form :model="form.model" :rules="form.rules" ref="formRef" label-width="120px">
        <el-form-item label="实际开票金额" prop="invoice_amount">
          <el-input
            type="number"
            :min="0"
            :step="0.01"
            class="sa-w-160"
            v-model="form.model.invoice_amount"
            placeholder="请输入实际开票金额"
            ><template #append>元</template></el-input
          >
        </el-form-item>
        <el-form-item label="上传发票图片" prop="download_urls">
          <sa-uploader v-model="form.model.download_urls" :multiple="true"></sa-uploader>
        </el-form-item>
      </el-form>
    </el-main>
    <el-footer class="sa-footer--submit">
      <el-button type="primary" @click="confirm">确 定</el-button>
    </el-footer>
  </el-container>
</template>
<script setup>
  import { cloneDeep } from 'lodash';
  import { onMounted, reactive, ref, unref } from 'vue';
  import { api } from '../order.service';
  const emit = defineEmits(['modalCallBack']);
  const props = defineProps({
    modal: {
      type: Object,
    },
  });
  // 添加 编辑 form
  let formRef = ref(null);
  const form = reactive({
    model: {
      invoice_amount: '',
      download_urls: [],
    },
    rules: {
      download_urls: [{ required: true, message: '请上传发票图片', trigger: 'blur' }],
      invoice_amount: [{ required: true, message: '请填写实际开票金额', trigger: 'blur' }],
    },
  });
  const loading = ref(false);
  // 表单关闭时提交
  async function confirm() {
    // 表单验证
    unref(formRef).validate(async (valid) => {
      if (!valid) return;
      let submitForm = cloneDeep(form.model);
      const { error } = await api.invoice.confirm(props.modal.params.data.id, submitForm);
      if (error == 0) {
        emit('modalCallBack', { event: 'confirm' });
      }
    });
  }

  onMounted(() => {
    form.model.invoice_amount = props.modal.params.data.amount;
  });
</script>
